package com.gl.emms.whale.ui;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Toast;

import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.nio.constant.EMMSConstant;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.nio.mutual.RequestBody;
import com.gl.emms.whale.R;
import com.gl.emms.whale.app.MyApplication;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.app.Constant;



public class SplanshActivity extends WhaleMonitorActivity{

	private Timer timer;//联网定时器
	private int trycnt = 0; //联网尝试次数
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		java.net.InetAddress address = null;
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());
		//连接服务端
		try {

			address=java.net.InetAddress.getByName(Constant.SERVER_DOMAINNAME);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			showToask("本机未连接网络");
			try {
				address = InetAddress.getLocalHost();
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();

		} 

		final View view = View.inflate(this, R.layout.activity_splansh, null);
		setContentView(view);
		AlphaAnimation aa = new AlphaAnimation(0.3f,1.0f);
		aa.setDuration(3000);
		view.startAnimation(aa);
		Log.v("whale", "解析的ip地址为："+address.getHostAddress()+"");
		EMMSPushManager.init(SplanshActivity.this,address.getHostAddress(), Constant.CIM_SERVER_PORT);
		//CIMPushManager.init(SplanshActivity.this,"192.168.0.101", Constant.CIM_SERVER_PORT);
		Log.v("whale", Constant.SERVER_DOMAINNAME+"");


		TimerTask task = new TimerTask(){   
			public void run() {
				trycnt++;
				if(trycnt>10)
				{
					//Toast.makeText(getApplicationContext(), "服务器未启动", 0).show();
					Message message = new Message();      
					message.what = 1;      
					handler.sendEmptyMessage(message.what);
					//timer.cancel();
				}


			}   
		};
		timer = new Timer(true);  
		timer.schedule(task,1000, 3000);

		//延时1000ms后执行，1000ms执行一次  
		//timer.cancel(); //退出Android计时器

	}
	final Handler handler = new Handler(){  
		public void handleMessage(Message msg) {  
			switch (msg.what) {      
			case 1: 
				showToask("服务器连接失败，网络未连接？");
				break;      
			}      
			super.handleMessage(msg);  
		}    
	};  
	/**
	 * 对网络连接状态进行判断
	 * @return  true, 可用； false， 不可用
	 */
	private boolean isOpenNetwork() {
		ConnectivityManager connManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		if(connManager.getActiveNetworkInfo() != null) {
			return connManager.getActiveNetworkInfo().isAvailable();
		}

		return false;
	}
	@Override
	public void onConnectionSucceed() {

		timer.cancel();
		//Toast.makeText(getApplicationContext(), "服务器连接成功", 0).show();
		showToask("服务器连接成功");

		//CIMPushManager.setID(this);
	}

	@Override
	public void onBackPressed() {
		timer.cancel();
		finish();
		EMMSPushManager.destory(this);
	}
	public String GetInetAddress(String  host){
		String IPAddress = ""; 
		InetAddress ReturnStr1 = null;
		try {
			ReturnStr1 = java.net.InetAddress.getByName(host);
			IPAddress = ReturnStr1.getHostAddress();
			System.out.println(IPAddress);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return  IPAddress;
		}
		return IPAddress;
	}
	//收到消息
	@Override
	public void onReplyReceived(ReplyBody reply) {
		super.onReplyReceived(reply);
		if(reply.getType().equals("id_response"))
		{
			Log.v("whale", "id_response");
			Intent intent = new Intent(SplanshActivity.this,LoginActivity.class);  
			startActivity(intent);
			finish();
		}

	}
	@Override
	public   void onNetworkChanged(NetworkInfo info)
	{
		Log.v("whale", "onNetworkChanged");
		java.net.InetAddress address = null;
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());
		//连接服务端
		try {

			address=java.net.InetAddress.getByName(Constant.SERVER_DOMAINNAME);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			showToask("本机未连接网络");
			try {
				address = InetAddress.getLocalHost();
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();

		} 
		Log.v("whale", "解析的ip地址为："+address.getHostAddress()+"");
		EMMSPushManager.init(SplanshActivity.this,address.getHostAddress(), Constant.CIM_SERVER_PORT);
		//CIMPushManager.init(SplanshActivity.this,"192.168.0.100", Constant.CIM_SERVER_PORT);
		Log.v("whale", Constant.SERVER_DOMAINNAME+"");
	}

}
